<template>
    <div class="manage-user">
        <div style="height: 35px">
            <el-form :model="searchForm" :inline="true" size="mini">
                <el-form-item label="name">
                    <el-input v-model="searchForm.name"></el-input>
                </el-form-item>
                <el-form-item label="code">
                    <el-input v-model="searchForm.code"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="listUser">Search</el-button>
                    <el-button type="primary" @click="listUser">Refresh</el-button>
                    <el-button type="primary" @click="handleCreate">New</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="manage-user-org">
            <el-table
                    @row-click="handleOrgRowClick"
                    v-infiniteScroll="infiniteScroll"
                    border
                    default-expand-all
                    highlight-current-row
                    :data="orgTable"
                    row-key="id"
                    height="500"
                    size="mini"
                    style="width: 100%"
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column
                        prop="name"
                        label="name"
                        width="*">
                </el-table-column>
            </el-table>
        </div>
        <div class="manage-user-table">
            <el-table
                    @row-dblclick="handleRowDblClick"
                    v-infiniteScroll="infiniteScroll"
                    border
                    highlight-current-row
                    row-key="ukId"
                    height="500"
                    size="mini"
                    :data="userTable"
                    style="width: 100%">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="account"
                        label="account"
                        width="*">
                </el-table-column>
                <el-table-column
                        prop="username"
                        label="username"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="status"
                        label="status"
                        width="70">
                    <template slot-scope="scope">
                        <el-switch
                                @change="statusChange($event, scope.row.ukId)"
                                :value="scope.row.status != '1'"
                                active-color="#909399"
                                inactive-color="#67c23a">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="sort"
                        label="sort"
                        width="60">
                </el-table-column>
                <el-table-column
                        label="操作"
                        width="170">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="handleEdit(scope.row)">编辑
                        </el-button>
                        <el-button
                                size="mini"
                                @click="handleReset(scope.row.ukId)">重置密码
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-drawer
                title="我是标题"
                :visible.sync="showUserDetails"
                :with-header="false">
            <span>我来啦!</span>
        </el-drawer>
        <el-dialog :close-on-click-modal="false" title="用户管理" :visible.sync="showUserForm" label-width="80px">
            <el-row>
                <el-col :span="12">
                    <el-form ref="form" :model="userForm" label-width="80px">
                        <el-form-item label="账号">
                            <el-input v-model="userForm.account" :disabled="isUpdate"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" v-if="!isUpdate">
                            <el-select v-model="userForm.passwordType" placeholder="密码类型" style="width: 110px">
                                <el-option
                                        v-for="item in this.$store.state.Pabu.dict.manage.PASSWORD_TYPE.dictItems"
                                        :key="item.ukId"
                                        :label="item.name"
                                        :value="parseInt(item.value)">
                                </el-option>
                            </el-select>
                            <el-input v-model="userForm.password" v-if="userForm.passwordType == '3'" style="width: calc(100% - 110px)"></el-input>
                        </el-form-item>
                        <el-form-item label="用户名">
                            <el-input v-model="userForm.username"></el-input>
                        </el-form-item>
                        <el-form-item label="角色">
                            <el-select v-model="userForm.roleIds" multiple placeholder="请选择角色" style="width: 100%">
                                <el-option
                                        v-for="item in roleTable"
                                        :key="item.ukId"
                                        :label="item.name"
                                        :value="item.ukId">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="所属机构">
                            <tree-select
                                    :showCount="true"
                                    :openOnClick="true"
                                    :defaultExpandLevel="3"
                                    noChildrenText="无下级机构"
                                    v-model="userForm.orgId"
                                    :options="orgTable"
                                    :normalizer="orgTableNormalizer">
                            </tree-select>
                        </el-form-item>
                        <el-form-item label="状态">
                            <el-select v-model="userForm.status" placeholder="请选择状态" style="width: 100%">
                                <el-option
                                        v-for="item in this.$store.state.Pabu.dict.manage.STATUS.dictItems"
                                        :key="item.ukId"
                                        :label="item.name"
                                        :value="parseInt(item.value)">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="排序">
                            <el-input v-model="userForm.sort" type="number" min="0" max="90000"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="handleSave">保存</el-button>
                            <el-button @click="showUserForm = false">取 消</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
                <el-col :span="12">
                    <el-form ref="form" :model="userForm" label-width="80px">
                        <el-form-item label="电话">
                            <el-input v-model="userForm.userInfo.telephone"></el-input>
                        </el-form-item>
                        <el-form-item label="手机号">
                            <el-input v-model="userForm.userInfo.phone"></el-input>
                        </el-form-item>
                        <el-form-item label="Email">
                            <el-input v-model="userForm.userInfo.email"></el-input>
                        </el-form-item>
                        <el-form-item label="地址">
                            <el-input v-model="userForm.userInfo.address"></el-input>
                        </el-form-item>
                        <el-form-item label="性别">
                            <el-select v-model="userForm.userInfo.gender" placeholder="请选择性别" style="width: 100%">
                                <el-option
                                        v-for="item in this.$store.state.Pabu.dict.manage.GENDER.dictItems"
                                        :key="item.ukId"
                                        :label="item.name"
                                        :value="parseInt(item.value)">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="年龄">
                            <el-input v-model="userForm.userInfo.age"></el-input>
                        </el-form-item>
                        <el-form-item label="生日">
                            <el-input v-model="userForm.userInfo.birthday"></el-input>
                        </el-form-item>
                        <el-form-item label="昵称">
                            <el-input v-model="userForm.userInfo.nickname"></el-input>
                        </el-form-item>
                        <el-form-item label="简介">
                            <el-input type="text" v-model="userForm.userInfo.introduction"></el-input>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
        </el-dialog>
    </div>
</template>

<script>
  import TreeSelect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'

  import {deleteOrg, listOrgTree} from "../../../api/manage/org";
  import {listUser, resetUserPassword, saveUser, updateUserStatus} from "../../../api/manage/user";
  import {listRole} from "../../../api/manage/role";
  import PageUtils from "../../../utils/PageUtils";

  export default {
    name: "User",
    components: {TreeSelect},
    data () {
      return {
        showUserForm: false,
        showUserDetails: false,
        searchForm: {},
        orgTable: [],
        roleTable: [],
        orgTableNormalizer (node) {
          if (!node.children.length) {
            delete node.children;
          }
          return {
            id: node.ukId,
            label: node.name
          }
        },
        userTable: [],
        userForm: {
          userInfo: {}
        },
        isUpdate: false
      }
    },
    mounted () {
    },
    methods: {
      handleCreate () {
        this.isUpdate = false
        this.userForm = {
          passwordType: 1,
          status: 1,
          userInfo: {}
        }
        this.listRole()
        this.showUserForm = true
      },
      handleEdit (row) {
        this.isUpdate = true
        this.userForm = row
        this.listRole()
        this.showUserForm = true
      },
      handleReset(userId) {
        resetUserPassword(userId).then(result => {
          this.listUser()
          this.showUserForm = false
        }).catch(error => {
          console.log(error)
        })
      },
      handleSave () {
        saveUser(this.userForm).then(result => {
          this.listUser()
          this.showUserForm = false
        }).catch(error => {
          console.log(error)
        })
      },
      handleDelete (ukId) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteOrg(ukId).then(result => {
            this.listOrgTree()
            this.showUserForm = false
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }).catch(error => {
            console.log(error)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      statusChange (value, userId) {
        let status = value ? 0 : 1
        updateUserStatus(userId, status).then(result => {
          this.listUser()
        }).catch(error => {
          console.log(error)
        })
      },
      handleRowDblClick () {
        this.showUserDetails = true
      },
      infiniteScroll () {
        let current = this.$store.state.Pabu.pagination.user.current + 1
        let size = this.$store.state.Pabu.pagination.user.size
        listUser(this.searchForm, current, size).then(result => {
          if (result.data.users.records.length !== 0) {
            for (let users of result.data.users.records) {
              this.userTable.push(users)
            }
            PageUtils.setPagination(this, "user", result.data.users)
          }
        }).catch(error => {
          console.log(error)
        })
      },
      handleOrgRowClick (row) {
        this.searchForm.orgId = row.ukId
        this.listUser()
      },
      listUser () {
        listUser(this.searchForm, 1, 30).then(result => {
          this.userTable = result.data.users.records
          PageUtils.setPagination(this, "user", result.data.users)
        }).catch(error => {
          console.log(error)
        })
      },
      listOrgTree () {
        listOrgTree("0").then(result => {
          this.orgTable = result.data.orgTree
        }).catch(error => {
          console.log(error)
        })
      },
      listRole () {
        listRole().then(result => {
          this.roleTable = result.data.roles.records
        })
      }
    },
    created () {
      this.listOrgTree()
      this.listUser()
    }
  }
</script>

<style>
    .manage-user {
        width: 100%;
        height: 100%;
    }

    .manage-user-org {
        width: 300px;
        height: calc(100% - 35px);
        float: left;
    }

    .manage-user-table {
        padding-left: 5px;
        width: calc(100% - 305px);
        height: calc(100% - 35px);
        float: left;
    }

    /* element ui样式重写 */
    .manage-user > .manage-user-org > .el-table,
    .manage-user > .manage-user-table > .el-table {
        height: calc(100% - 5px) !important;
    }

    .manage-user > .manage-user-table > .el-table > .el-table__body-wrapper {
        height: calc(100% - 5px) !important;
    }
</style>
